package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.lease.web.admin.mapper.ApartmentInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
* @author 10740
* @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
* @createDate 2024-06-18 18:20:57
*/
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
    implements ApartmentInfoService{

    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    ApartmentFacilityService apartmentFacilityService;
    @Autowired
    ApartmentLabelService apartmentLabelService;
    @Autowired
    ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    GraphInfoService graphInfoService;
    @Autowired
    ProvinceInfoService provinceInfoService;
    @Autowired
    CityInfoService cityInfoService;
    @Autowired
    DistrictInfoService districtInfoService;
    @Autowired
    FacilityInfoService facilityInfoService;
    @Autowired
    LabelInfoService labelInfoService;
    @Autowired
    FeeValueService feeValueService;
    @Autowired
    RoomInfoService roomInfoService;


    //分页查询
    @Override
    public IPage<ApartmentItemVo> pageItem(IPage<ApartmentItemVo> iPage, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageItem(iPage, queryVo);
    }

    //添加或者修改公寓信息
    @Override
    public void saveOrUpdateInfo(ApartmentSubmitVo apartmentSubmitVo) {
        ProvinceInfo provinceInfo = provinceInfoService.getById(apartmentSubmitVo.getProvinceId());
        CityInfo cityInfo = cityInfoService.getById(apartmentSubmitVo.getCityId());
        DistrictInfo districtInfo = districtInfoService.getById(apartmentSubmitVo.getDistrictId());

        apartmentSubmitVo.setProvinceName(provinceInfo.getName());
        apartmentSubmitVo.setCityName(cityInfo.getName());
        apartmentSubmitVo.setDistrictName(districtInfo.getName());

        //获取被修改的公寓id
        Long apartmentId = apartmentSubmitVo.getId();
        boolean flag = false;
        if (apartmentId != null){//判断是否是修改
            flag = true;
        }

        //添加 | 修改公寓基本信息
        saveOrUpdate(apartmentSubmitVo);

        if (flag){ //是修改，先删除公寓关联的其他外键表数据
            //删除配套关系表数据
            LambdaQueryWrapper<ApartmentFacility> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ApartmentFacility::getApartmentId, apartmentId);
            apartmentFacilityService.remove(queryWrapper);
            //删除标签关系表数据
            apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>()
                    .eq(ApartmentLabel::getApartmentId, apartmentId));
            //删除杂费关系表数据
            apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>()
                    .eq(ApartmentFeeValue::getApartmentId, apartmentId));
            //删除图片关系表数据
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>()
                    .eq(GraphInfo::getItemId, apartmentId).eq(GraphInfo::getItemType, ItemType.APARTMENT));

        }

        //添加相关数据
        //配套   facilityInfoIds: 所有配套信息的id
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)){
            //关系表中的所有数据
            List<ApartmentFacility> facilities = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                ApartmentFacility apartmentFacility = ApartmentFacility.builder()
                        .apartmentId(apartmentSubmitVo.getId())
                        .facilityId(facilityInfoId).build();
                facilities.add(apartmentFacility);
            }
            apartmentFacilityService.saveBatch(facilities);
        }
        //标签
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIds)){
            List<ApartmentLabel> labels = new ArrayList<>();
            for (Long labelId : labelIds) {
                ApartmentLabel apartmentLabel = ApartmentLabel.builder()
                        .apartmentId(apartmentSubmitVo.getId())
                        .labelId(labelId).build();
                labels.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(labels);
        }
        //杂费
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds)){
            List<ApartmentFeeValue> feeValues = new ArrayList<>();
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder()
                        .apartmentId(apartmentSubmitVo.getId())
                        .feeValueId(feeValueId).build();
                feeValues.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(feeValues);
        }
        //图片
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)){
            List<GraphInfo> graphInfos = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfos.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfos);
        }

    }

    //回显
    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        //查询公寓基本信息
        ApartmentInfo apartmentInfo = super.getById(id);

        //查询公寓配套信息
        List<FacilityInfo> facilityInfos = facilityInfoService.getFacilityInfo(id);

        //查询公寓标签信息
        List<LabelInfo> labelInfos = labelInfoService.getLabelInfo(id);

        //查询公寓杂费信息
        List<FeeValueVo> feeValueInfos = feeValueService.getFeeInfo(id);

        // feeValueInfos.forEach(System.out::println);
        //查询公寓图片信息
        List<GraphVo> graphInfos = graphInfoService.getGraphInfo(ItemType.APARTMENT,id);
        // graphInfos.forEach(System.out::println);

        apartmentDetailVo.setFacilityInfoList(facilityInfos);
        apartmentDetailVo.setLabelInfoList(labelInfos);
        apartmentDetailVo.setFeeValueVoList(feeValueInfos);
        apartmentDetailVo.setGraphVoList(graphInfos);

        //把 apartmentInfo 中的属性值 复制到 apartmentDetailVo 中对应的属性上
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);

        return apartmentDetailVo;
    }

    //删除公寓
    @Override
    public void removeByIdInfo(Long apartmentId) {
        //若公寓有关联的房间，该公寓不能删除
        long count = roomInfoService.count(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getApartmentId, apartmentId));

        if (count > 0){
            throw new LeaseException("公寓有关联的房间，该公寓不能删除",201);
        }


        //删除配套关系表数据
        LambdaQueryWrapper<ApartmentFacility> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApartmentFacility::getApartmentId, apartmentId);
        apartmentFacilityService.remove(queryWrapper);
        //删除标签关系表数据
        apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>()
                .eq(ApartmentLabel::getApartmentId, apartmentId));
        //删除杂费关系表数据
        apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>()
                .eq(ApartmentFeeValue::getApartmentId, apartmentId));
        //删除图片关系表数据
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>()
                .eq(GraphInfo::getItemId, apartmentId
                ).eq(GraphInfo::getItemType, ItemType.APARTMENT));

        //删除公寓的基本信息
        super.removeById(apartmentId);

    }
}




